package com.hlabexamples.fabrevealmenu;

import com.gc.materialdesign.views.ButtonFloat;
import com.gc.materialdesign.views.CheckBox;
import com.hlab.fabrevealmenu.view.FABRevealMenu;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * ScrollingAbility
 *
 * @since 2021-05-16
 */
public class ScrollingAbility extends Ability {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "ScrollingAbility");
    private static final String COLOR = "#1976d2";
    private static final int OVERSCROLL_PERCENT = 20;
    private static final float OVERSCROLL_RATE = 1.0f;
    private static final int REMAIN_VISIBLE_PERCENT = 20;
    private static final int PROFILE_SIZE = 108;
    private static final float FLOAT_095 = 0.95f;
    private final String[] strings = new String[]{"Item 1","Item 2","Item 3","Item 4"};
    private final int[] textItemIds = new int[]{ResourceTable.Id_text_item1,ResourceTable.Id_text_item2,
        ResourceTable.Id_text_item3,ResourceTable.Id_text_item4};
    private FABRevealMenu fabMenu;
    private DirectionalLayout dlToast;
    private Text[] textItems = new Text[Constants.INT_4];

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get().setStatusBarColor(Color.getIntColor(COLOR));
        setUIContent(ResourceTable.Layout_ability_scrolling);
        Image appbarBackBtn = (Image) findComponentById(ResourceTable.Id_appbar_backbtn);
        appbarBackBtn.setClickedListener(component -> terminateAbility());
        initScrollView();
    }

    private void initScrollView() {
        ScrollView scrollView = (ScrollView) findComponentById(ResourceTable.Id_contacts_detail_scroll);
        int profileSizePx = AttrHelper.vp2px(PROFILE_SIZE, this);
        if (scrollView != null) {
            scrollView.setReboundEffectParams(OVERSCROLL_PERCENT, OVERSCROLL_RATE, REMAIN_VISIBLE_PERCENT);
            scrollView.setReboundEffect(false);
            Text userName = (Text) findComponentById(ResourceTable.Id_appbar_username);
            Text contactsDetailTitle = (Text) findComponentById(ResourceTable.Id_contacts_detail_title);
            ButtonFloat fab = (ButtonFloat) findComponentById(ResourceTable.Id_fab);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setShape(ShapeElement.RECTANGLE);

            // Set Scrolled listener
            scrollView.getComponentTreeObserver().addScrolledListener(() -> {
                float curY = scrollView.getScrollValue(Component.AXIS_Y);
                int fixedProfileSize = (int) (profileSizePx * (profileSizePx - curY)) / profileSizePx;
                if (fixedProfileSize > profileSizePx) {
                    curY = 0;
                } else if (fixedProfileSize < 0) {
                    curY = profileSizePx;
                }
                contactsDetailTitle.setAlpha(1 - curY / profileSizePx);
                contactsDetailTitle.setScale(1 - curY / profileSizePx,1 - curY / profileSizePx);
                fab.setAlpha(1 * (profileSizePx - curY) / profileSizePx);
                if (curY / profileSizePx >= FLOAT_095) {
                    userName.setAlpha(1);
                } else {
                    userName.setAlpha(0);
                }
            });
            fabMenu = (FABRevealMenu) findComponentById(ResourceTable.Id_fabMenu);
            if (fab != null && fabMenu != null) {
                ComponentContainer customView = (ComponentContainer)LayoutScatter
                        .getInstance(getContext()).parse(ResourceTable.Layout_layout_custom_menu, null, false);
                setupCustomFilterView(customView);
                fabMenu.setCustomView(customView);
                fabMenu.bindAnchorView(fab);
            }
        }
    }

    private void setupCustomFilterView(ComponentContainer customView) {
        if (customView != null) {
            DependentLayout dlCb1 = (DependentLayout)customView
                    .findComponentById(ResourceTable.Id_dl_cb1);
            CheckBox cb1 = (CheckBox)customView.findComponentById(ResourceTable.Id_cb1);
            cb1.setBackgroundColor(COLOR);
            dlCb1.setClickedListener(component -> cb1.setChecked(!cb1.isCheck()));
            DependentLayout dlCb2 = (DependentLayout)customView
                    .findComponentById(ResourceTable.Id_dl_cb2);
            CheckBox cb2 = (CheckBox)customView.findComponentById(ResourceTable.Id_cb2);
            cb2.setBackgroundColor(COLOR);
            dlCb2.setClickedListener(component -> cb2.setChecked(!cb2.isCheck()));
            DependentLayout dlCb3 = (DependentLayout)customView
                    .findComponentById(ResourceTable.Id_dl_cb3);
            CheckBox cb3 = (CheckBox)customView.findComponentById(ResourceTable.Id_cb3);
            cb3.setBackgroundColor(COLOR);
            dlCb3.setClickedListener(component -> cb3.setChecked(!cb3.isCheck()));
            DependentLayout dlCb4 = (DependentLayout)customView
                    .findComponentById(ResourceTable.Id_dl_cb4);
            CheckBox cb4 = (CheckBox)customView.findComponentById(ResourceTable.Id_cb4);
            cb4.setBackgroundColor(COLOR);
            dlCb4.setClickedListener(component -> cb4.setChecked(!cb4.isCheck()));
            final CheckBox[] filters = new CheckBox[]{cb1, cb2, cb3, cb4};
            Button btnApply = (Button)customView.findComponentById(ResourceTable.Id_btnApply);
            btnApply.setClickedListener(view -> {
                fabMenu.closeMenu();
                for (int i = 0;i < filters.length;i++) {
                    CheckBox filter = filters[i];
                    if (filter.isCheck()) {
                        textItems[i].setText(strings[i]);
                        textItems[i].setVisibility(Component.VISIBLE);
                    } else {
                        textItems[i].setText("");
                        textItems[i].setVisibility(Component.HIDE);
                    }
                }
                dlToast.setVisibility(Component.VISIBLE);
                getMainTaskDispatcher().delayDispatch(() -> dlToast.setVisibility(Component.HIDE), Constants.INT_3000);
            });
            initToast();
        }
    }

    private void initToast() {
        dlToast = (DirectionalLayout)findComponentById(ResourceTable.Id_dl_toast);
        for (int i = 0;i < Constants.INT_4;i++) {
            textItems[i] = (Text)findComponentById(textItemIds[i]);
        }
    }
}